package com.example.wanbaqian.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.example.wanbaqian.R;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * Created by Administrator on 2017/4/7.
 */

public class CardView extends AutoRelativeLayout {
    private String money = "5";
    boolean showimg = false;
    private float radius = 10;
    private float gap = 5;
    private int radiusBackgroundColor = Color.WHITE;
    private int BackgroundColor = Color.RED;
    private Paint paint;
    private int vNums = 10;
    private float widthpercent = 0.3f;

    public CardView(Context context) {
        this(context, null);
    }

    public CardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setWillNotDraw(false);//必须
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        paint.setColor(radiusBackgroundColor);
        RectF oval = new RectF(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(oval, dip2px(getContext(), 5), dip2px(getContext(), 5), paint);
        paint.setColor(BackgroundColor);
        RectF oval2 = new RectF(0, 0, getWidth() * widthpercent, getHeight());
        canvas.drawRoundRect(oval2, dip2px(getContext(), 5), dip2px(getContext(), 5), paint);
        canvas.drawRect(60, 0, getWidth() * widthpercent, getHeight(), paint);
        paint.setColor(radiusBackgroundColor);
        for (int i = 0; i < vNums; i++) {
            canvas.drawCircle(getWidth() * widthpercent, radius + i * (2 * radius + gap) + gap, radius, paint);
        }
        paint.setTextSize(getHeight() * 0.3f);
        float moneyleft;
        if (money.length() >= 3)
            moneyleft = getHeight() * 0.1f;
        else
            moneyleft = getHeight() * 0.2f;
        canvas.drawText(money + " 元", moneyleft, getHeight() * 0.2f + getHeight() * 0.25f, paint);
        paint.setTextSize(getHeight() * 0.1f);
        canvas.drawText("现金抵扣券", getHeight() / 5, getHeight() * 0.2f + (getHeight() * 0.2f) + getHeight() * 0.35f, paint);

        if (showimg) {
            paint = new Paint();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.find_used);
            Matrix mMatrix = new Matrix();
            mMatrix.postScale((float) getHeight() * 0.45f / bitmap.getWidth(),
                    (float) getHeight() * 0.45f / bitmap.getHeight());
            mMatrix.postTranslate(getWidth() - getHeight() * 0.45f * 1.5f, getHeight() / 2 - getHeight() * 0.45f / 2);
            canvas.drawBitmap(bitmap, mMatrix, null);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        radius = ((getHeight() - gap * (vNums + 1)) / vNums) / 2;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public CardView setColor(int Color) {
        BackgroundColor = Color;
        return this;
    }

    public CardView setMoney(String m) {
        money = m;
        return this;
    }

    public CardView setIsused(Boolean b) {
        showimg = b;
        BackgroundColor = Color.parseColor(b ? "#BEBEBE" : "#FE2F4D");
        return this;
    }

    public void Build() {
        invalidate();
    }
}
